<template>
  <Main :type="'form'">
    <el-form ref="form" label-width="150px" :model="form" :rules="form">
      <Title :title="'余额设置'" />
      <el-form-item label="可用人群">
        <el-radio v-model="form.member_auth" :label="1">会员</el-radio>
        <el-radio v-model="form.member_auth" :label="2">客户</el-radio>
      </el-form-item>
      <el-form-item label="充值方式">
        <el-checkbox-group v-model="form.recharge_mode">
          <el-checkbox label="1" disabled>后台充值</el-checkbox>
          <el-checkbox label="2">手机端充值</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="充值金额">
        <el-radio v-model="form.recharge_type" :label="1"
          >固定充值金额</el-radio
        >
        <el-radio v-model="form.recharge_type" :label="2"
          >自定义充值金额</el-radio
        >
        <div v-if="form.recharge_type === 1" class="list">
          <div v-for="(item, index) in list" :key="index" class="list-chlid">
            {{ item }}元
            <i class="el-icon-error" @click="del(index)"></i>
          </div>
          <el-popover v-model="visible" placement="top" width="210">
            <el-input-number
              v-model="money"
              :min="1"
              :precision="0"
              :controls="false"
            ></el-input-number>
            <div style="text-align: right; margin-top: 10px;">
              <el-button size="mini" type="text" @click="out">取消</el-button>
              <el-button type="primary" size="mini" @click="add"
                >确定</el-button
              >
            </div>
            <el-button slot="reference" type="primary" style="margin-top: 5px;"
              >添加金额</el-button
            >
          </el-popover>
        </div>
        <div v-else>
          <el-input-number
            v-model="form.recharge_tyep_content"
            :min="1"
            :precision="0"
            :controls="false"
          ></el-input-number
          >&nbsp;&nbsp;元
        </div>
      </el-form-item>
      <el-form-item label="使用方式">
        <el-checkbox-group v-model="form.use_mode">
          <el-checkbox label="1" disabled>后台充值</el-checkbox>
          <el-checkbox label="2">手机端充值</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <div v-if="form.use_mode.length == 2">
        <el-form-item label="单次使用限制">
          <div>
            <el-checkbox v-model="form.is_limit_min">最少使用</el-checkbox
            >&nbsp;&nbsp;
            <el-input-number
              v-model="form.deduction_limit_min"
              :min="1"
              :precision="0"
              :disabled="!form.is_limit_min"
              :controls="false"
            ></el-input-number
            >&nbsp;&nbsp;余额
          </div>
          <div style="margin-top:20px;">
            <el-checkbox v-model="form.is_limit_max">最多使用</el-checkbox
            >&nbsp;&nbsp;
            <el-input-number
              v-model="form.deduction_limit_max"
              :min="1"
              :precision="0"
              :disabled="!form.is_limit_max"
              :controls="false"
            ></el-input-number
            >&nbsp;&nbsp;余额
          </div>
        </el-form-item>
        <el-form-item label="抵扣项目">
          <el-checkbox-group v-model="form.deduction_type">
            <el-checkbox label="1">订单金额</el-checkbox>
            <el-checkbox label="2">订单运费</el-checkbox>
          </el-checkbox-group>
          <span class="add-info">纯运费金额，与商品无关</span>
        </el-form-item>
        <el-form-item label="适用商品">
          <el-radio-group v-model="form.apply_type" @change="DataArr = []">
            <el-radio :label="0">全部商品参与</el-radio>
            <el-radio :label="1">指定商品参与</el-radio>
            <el-radio :label="2">指定商品不参与</el-radio>
          </el-radio-group>
          <div v-if="form.apply_type != 0">
            <shop-new :data-arr="DataArr" @close="close" />
          </div>
        </el-form-item>
        <el-form-item label="订单抵扣比例">
          <el-checkbox v-model="form.is_deduction_bili"
            >单次消费最多抵扣订单金额的</el-checkbox
          >&nbsp;&nbsp;
          <el-input-number
            v-model="form.deduction_bili"
            :min="1"
            :max="100"
            :precision="0"
            :disabled="!form.is_deduction_bili"
            :controls="false"
          ></el-input-number
          >&nbsp;&nbsp;%
        </el-form-item>
      </div>
    </el-form>
    <Footer>
      <el-button type="primary" @click="submit">保 存</el-button>
    </Footer>
  </Main>
</template>
<script>
import { SETYUE } from "../../service";

export default {
  components: {
    shopNew: () => import("publicCom/shopNew")
  },
  data() {
    return {
      visible: false,
      dialogVisible: false,
      dataJson: {},
      money: 1,
      list: [],
      DataArr: [],
      form: {
        member_auth: 1,
        recharge_mode: [1],
        recharge_type: 1,
        recharge_tyep_content: "",
        use_mode: [1],
        is_limit_min: false,
        is_limit_max: false,
        deduction_limit_min: "",
        deduction_limit_max: "",
        deduction_type: [],
        is_deduction_bili: false,
        deduction_bili: "",
        apply_type: 0
      }
    };
  },
  created() {
    this.get();
  },

  methods: {
    close(e) {
      if (e) {
        this.DataArr = e;
      }
    },
    add() {
      let soft = this.list.includes(this.money);
      if (soft) {
        this.$message.error("金额不能重复，请重新填写！");
      } else {
        this.list.push(this.money);
        this.list.sort();
        this.money = 1;
        this.visible = false;
      }
    },
    del(i) {
      this.list.splice(i, 1);
    },
    out() {
      this.money = 1;
      this.visible = false;
    },
    async submit() {
      if (this.form.recharge_type === 1) {
        this.form.recharge_tyep_content = this.list.join(",");
      }
      let arr = [];
      this.DataArr.forEach(ele => {
        arr.push(ele.goods_id);
      });
      await this.$refs["form"].validate(valid => {
        if (valid) {
          SETYUE({
            flag: "save",
            member_auth: this.form.member_auth,
            recharge_mode: this.form.recharge_mode.join(","),
            recharge_type: this.form.recharge_type,
            recharge_tyep_content: this.form.recharge_tyep_content,
            use_mode: this.form.use_mode.join(","),
            is_limit_min: this.form.is_limit_min ? 1 : 0,
            deduction_limit_min: this.form.deduction_limit_min,
            is_limit_max: this.form.is_limit_max ? 1 : 0,
            deduction_limit_max: this.form.deduction_limit_max,
            deduction_type: this.form.deduction_type.join(","),
            is_deduction_bili: this.form.is_deduction_bili ? 1 : 0,
            deduction_bili: this.form.deduction_bili,
            apply_type: this.form.apply_type,
            apply_goods: arr.join(",")
          }).then(res => {
            let { code, msg } = res;
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        }
      });
    },
    async get() {
      await SETYUE().then(res => {
        let { code, data, msg } = res;
        if (code === 200) {
          if (!data) return;
          this.form = {
            member_auth: data.member_auth,
            recharge_mode: data.recharge_mode.split(","),
            recharge_type: data.recharge_type,
            use_mode: data.use_mode.split(","),
            is_limit_min: data.assets_rule.is_limit_min === 1,
            deduction_limit_min: data.assets_rule.deduction_limit_min,
            is_limit_max: data.assets_rule.is_limit_max === 1,
            deduction_limit_max: data.assets_rule.deduction_limit_max,
            deduction_type: data.assets_rule.deduction_type
              ? data.assets_rule.deduction_type.split(",")
              : [],
            is_deduction_bili: data.assets_rule.is_deduction_bili === 1,
            deduction_bili: data.assets_rule.deduction_bili,
            apply_type: data.assets_rule.apply_type
          };
          this.DataArr = data.assets_rule.goods;
          if (data.recharge_type === 1) {
            this.list = data.recharge_tyep_content.split(",");
          } else {
            this.form.recharge_tyep_content = data.recharge_tyep_content;
          }
        } else {
          this.$message.error(msg);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.list {
  display: flex;
  .list-chlid {
    width: 110px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    border: 1px solid #d7d7d7;
    border-radius: 6px;
    margin-right: 20px;
    position: relative;
    i {
      position: absolute;
      top: 18px;
      right: 6px;
      cursor: pointer;
      display: none;
    }
    &:hover i {
      display: inline-block;
    }
  }
}
</style>
